﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Net;

public partial class Tranfer : Basepage
{
    //khoi tao ket noi csdl
    static string strconn = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(strconn);
    protected void Page_Load(object sender, EventArgs e)
    {
        txtErrorMessageCustomer.Visible = false;
    }
    protected void btTranfer_Click(object sender, EventArgs e)
    {
        string url = this.Page.Request.Url.AbsoluteUri;
        string username = Request.Cookies["username"].Value;
        if(txtpassCustomer.Text.Trim()==""||txtCardIDCustomer.Text.Trim()=="" || txtImountCustomer.Text.Trim()=="")
        {            
                if (url.Contains("lang=vi"))
                    txtErrorMessageCustomer.Text = "Xin điền đầy đủ thông tin.";                  
                else
                    txtErrorMessageCustomer.Text = "You miss any box.";
                txtErrorMessageCustomer.Visible = true;
                return;         
        }
        //call webservice login
        moigioinama.Service1 moigioi = new moigioinama.Service1();
        moigioi.CookieContainer = new CookieContainer();
        ServicePointManager.Expect100Continue = false;
        string sID = moigioi.Login(username, txtpassCustomer.Text.Trim());
        
        if (sID == "0")
        {
            if (url.Contains("lang=vi"))
                txtErrorMessageCustomer.Text = "Đăng nhập Webservice thất bại.";
            else
                txtErrorMessageCustomer.Text = "Login Webservice Fail.";
            return;
        }
        //call webservice transfer for customer
        int imount = int.Parse(txtImountCustomer.Text.Trim());
        int fee = moigioi.TranferMoneyForCustomer(sID, txtCardIDCustomer.Text.Trim(), imount);
       

        if (fee <= 0)
        {
            if(url.Contains("lang=vi"))
                txtErrorMessageCustomer.Text = "Chuyển Tiền Thất Bại! Vui lòng thử lại sau !";
            else
                txtErrorMessageCustomer.Text="Transfer Fail! Please try again later!";
        }
        else
        {
           
            if(url.Contains("lang=vi"))
                Response.Redirect("AfterTransfer.aspx?lang=vi");
            else
                Response.Redirect("AfterTransfer.aspx?lang=en");
        }
    }
}